package app.view.component.popup 
{
	import app.facade.App;
	import com.temple.core.CoreMovieClip;
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filters.DropShadowFilter;
	import flash.filters.GlowFilter;
	import app.view.component.Utils;
	import com.greensock.easing.*;
	import com.greensock.*
	import com.greensock.plugins.*;
	import app.view.component.buttons.BtnClose;
	import flash.utils.Timer;
	/**
	 * ...
	 * @author PhongVd
	 */
	public class Popup extends CoreMovieClip
	{
		public static var listPopup:Array 	= [];
		
		//ANIMATION TYPE
		private static const FADE_IN_SCALE:int = 1;
		private static const FLY_IN_FROM_LEFT:int = 2;
		private var animationType:int = 1;
		
		public var btnClose:BtnClose;
		public var onClose:Function;
		public var noOpenAnimate:Boolean = false;
		
		private var timeClose:int = 3; 									/*thời gian đê close form*/
		public var alwaysShow:Boolean = false;							/*Luôn bât popup này*/
		
		public function Popup() {
			super();
		}
		
		public function initialize(onClose:Function = null, btClose:BtnClose =null, type:int = 1, delayClose:Boolean = true):void {
			this.animationType = type;
			this.onClose = onClose;
			if (btClose) {
				this.btnClose = btClose;
				this.btnClose.addEventListener(MouseEvent.CLICK, closeSelf);
				this.addChild(btnClose);
			}
			if (delayClose) {
				TweenLite.delayedCall(timeClose, closeSelf);
			}	
		}
		
		public function onInitalPopUp():void {		
			if (!noOpenAnimate){
				switch (animationType) {
					case FADE_IN_SCALE:
						this.alpha = 0;
						this.scaleX = 0.5;
						this.scaleY = 0.5;
						TweenMax.to(this, 0.3, { scaleX:1, scaleY:1 , alpha:1 } );
						break;
					case FLY_IN_FROM_LEFT:
						var desX:Number = loaderInfo.width/2;
						this.x = 0 - this.width/2 - 50;
						TweenMax.to(this, 0.5, { x:desX, ease:Back.easeOut } );
						break;
					default:
						break;
				}
				
			}
			this.filters = [new DropShadowFilter(0, 0, 0x000000, 0.5, 10, 10, 2, 2)];
			
			setVisiableTopPopup(false);
			listPopup.push(this);
		}
		
		private function closeSelf(evt:MouseEvent = null):void {
			var thisParent:DisplayObject = this.parent;
			switch (animationType) {
				case FADE_IN_SCALE:
					TweenMax.to(this, 0.6, {  scaleX:0.5, scaleY:0.5 , alpha:0, ease:Back.easeIn, onComplete:completeTweenCloseForm, onCompleteParams:[thisParent, this] } );		
					break;
				case FLY_IN_FROM_LEFT:
					TweenMax.to(this, 0.6, { x:loaderInfo.width + this.width/2 + 50, ease:Back.easeIn, onComplete:completeTweenCloseForm, onCompleteParams:[thisParent, this] } );		
					break;
				default:
					break;
			}
			
			listPopup.splice(listPopup.length - 1, 1);
			setVisiableTopPopup(true);
			
		}
		
		/**
		 * kiểm tra xem popup nào đang mở thì visiable lại
		 */
		private function setVisiableTopPopup(value:Boolean):void {
			if (listPopup.length > 0) {
				if (listPopup[listPopup.length - 1].alwaysShow == false) {
					listPopup[listPopup.length - 1].visible = value;
				}
				
			}
		}
		
		private function completeTweenCloseForm(_parent:*, _this:*):void { 
			sendNotification(App.HIDE_BACK_SHADER);
			if (onClose!=null) {
				onClose();
			}
		}
	}

}